package com.issac.vhr.controller.system.basic;

import com.issac.vhr.entity.Menu;
import com.issac.vhr.entity.RespBean;
import com.issac.vhr.entity.Role;
import com.issac.vhr.service.MenuService;
import com.issac.vhr.service.RoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.PushBuilder;
import java.util.List;

@RestController
@RequestMapping("/system/basic/role/")
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    MenuService menuService;

    @GetMapping("/")
    public List<Role> getAllRoles() {
        return roleService.getAllRoles();
    }

    @GetMapping("/menus")
    public List<Menu> getAllMenus() {
        return menuService.getAllMenus();
    }

    @GetMapping("/menus/{rid}")
    public List<Integer> getMidsByRid(@PathVariable Integer rid) {
        return menuService.getMidsByRid(rid);
    }

    /*
     * 根据当前rid把已有mid记录全部删除，再批量添加mid
     *
     * */
    @PutMapping("/menus/")
    public RespBean updateMenuRole(@Param("rid") Integer rid, @Param("mids") Integer[] mids) {
        if (menuService.updateMenuRole(rid, mids)) {
            return RespBean.ok("修改成功");
        } else {
            return RespBean.error("修改失败");
        }
    }

    @PostMapping("/")
    public RespBean addRole(@RequestBody Role role) {
        if (roleService.addRole(role) == 1) {
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteRole(@PathVariable Integer id) {
        if (roleService.deleteRole(id) == 1) {
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

}
